package com.dhcc.core.modules.system.warpper;

import java.text.DecimalFormat;
import java.util.Map;

import com.dhcc.core.framework.base.warpper.BaseControllerWarpper;
import com.dhcc.core.modules.system.cache.dict.DictCache;

/**
 * 个人文件的包装类
 *
 * @ClassName: PanFileWarpper
 * @Description: TODO
 * @author: mk
 * @date: 2018年8月30日 上午10:43:28
 */
public class PanFileWarpper extends BaseControllerWarpper {

    public PanFileWarpper(Object obj) {
        super(obj);
    }

    @Override
    protected void warpTheMap(Map<String, Object> map) {
        if(map.size() > 0){
            map.put("fileName",(String)map.get("originalName"));
            map.put("fileType",(String)map.get("fileMimeType"));
            map.put("fileSize",getFileSize((Long) map.get("fileSize")));
            map.put("fileSource", DictCache.me().findDictNameByPcodeAndKey("source", (String)map.get("source")));

        }
    }

    public static String getFileSize(Long filesize){
        String fileSizeString = "";
        if(filesize == 0){
            fileSizeString = "0B";
        }else {
            DecimalFormat df = new DecimalFormat("#.00");

            if (filesize < 1024) {
                fileSizeString = df.format((double) filesize) + "B";
            } else if (filesize < 1048576) {
                fileSizeString = df.format((double) filesize / 1024) + "KB";
            } else if (filesize < 1073741824) {
                fileSizeString = df.format((double) filesize / 1048576) + "MB";
            }
        }
        return fileSizeString;
    }
}
